//
//  XJ_VisionInfoViewController.m
//  XunJieProject
//
//  Created by xue on 2018/6/8.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_VersionInfoViewController.h"
#import "XJ_RealNameAuthView.h"

@interface XJ_VersionInfoViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *up_logo;
@property (weak, nonatomic) IBOutlet UILabel *versionLabel;

@end

@implementation XJ_VersionInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.versionLabel.text =[NSString stringWithFormat:@"当前版本 %@",APPVERSION];
}

-(void)creatMainUI{
    
    self.nav_titleLabel.text =@"版本信息";
    _up_logo.constant =SafeAreaTopHeight+48;
}

- (IBAction)Click_btn:(id)sender {
    
    [self loadUserCheckVersion];
}
#pragma mark--检测更新接口
-(void)loadUserCheckVersion{
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:USERID forKey:@"userId"];
    [dic setValue:token forKey:@"token"];
    
    [XJ_Utils signPost:@"user/checkVersion?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        NSDictionary * iosVersionInfo = [dataDic objectForKey:@"iosVersionInfo"];
        NSString *updateLink =iosVersionInfo[@"updateLink"];//下载地址
        
        /////versionUpdateFlag是否更新：0：不需要更新 1：选择更新 2：强制更新
        NSInteger versionUpdateFlag =[iosVersionInfo[@"versionUpdateFlag"] integerValue];
        
        //线上构建版本
        NSInteger buildVersion = [iosVersionInfo[@"buildVersion"] integerValue];
        
        //本地构建版本
        NSString *localVersion = [[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleVersion"];
        
        if ([localVersion integerValue] < buildVersion ) {
            
            if (versionUpdateFlag ==1) {//选择更新
                
                XJ_RealNameAuthView *alext =[[XJ_RealNameAuthView alloc]initWithTitle:@"提示" Message:@"发现新版本，是否要更新？" sureBtn:@"确定" cancleBtn:@"取消"];
                alext.resultIndex = ^(NSInteger index) {
                    
                    if (index==2) {//到App Store下载页
                        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",updateLink]];
                        
                        [[UIApplication sharedApplication] openURL:url];
                    }
                    
                };
                [alext showXLAlertView];
                
                
            }else if (versionUpdateFlag ==2)//强制更新
            {
                XJ_RealNameAuthView *alext =[[XJ_RealNameAuthView alloc]initWithTitle:@"提示" Message:@"发现新版本，是否要更新？" sureBtn:@"确定" cancleBtn:nil];
                alext.resultIndex = ^(NSInteger index) {
                    
                    
                    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",updateLink]];
                        
                    [[UIApplication sharedApplication] openURL:url];
                    
                };
                [alext showXLAlertView];
                
                
            }else if(versionUpdateFlag == 0)//不需要提醒
            {
//                return ;
            }
            
        }
        
         if ([localVersion integerValue] == buildVersion ) {
             
             XJ_RealNameAuthView *alext =[[XJ_RealNameAuthView alloc]initWithTitle:@"软件更新" Message:[NSString stringWithFormat:@"当前版本%@ 已是最新版本，无需更新",APPVERSION] sureBtn:nil cancleBtn:@"确定"];
             [alext showXLAlertView];
         }
        
//        if (versionUpdateFlag==0) {//不需要更新
//            XJ_RealNameAuthView *alext =[[XJ_RealNameAuthView alloc]initWithTitle:@"软件更新" Message:[NSString stringWithFormat:@"当前版本%@ 已是最新版本，无需更新",APPVERSION] sureBtn:nil cancleBtn:@"确定"];
//            [alext showXLAlertView];
//
//        }else if (versionUpdateFlag ==1){//选择更新
//
//            XJ_RealNameAuthView *alext =[[XJ_RealNameAuthView alloc]initWithTitle:@"提示" Message:@"发现新版本，是否要更新？" sureBtn:@"确定" cancleBtn:@"取消"];
//            alext.resultIndex = ^(NSInteger index) {
//
//                if (index==2) {//到App Store下载页
//                    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",updateLink]];
//
//                    [[UIApplication sharedApplication] openURL:url];
//                }
//
//            };
//            [alext showXLAlertView];
//
//
//        }else if (versionUpdateFlag ==2){//强制更新
//            XJ_RealNameAuthView *alext =[[XJ_RealNameAuthView alloc]initWithTitle:@"提示" Message:@"发现新版本，是否要更新？" sureBtn:@"确定" cancleBtn:@"退出"];
//            alext.resultIndex = ^(NSInteger index) {
//
//                if (index==1) {//退出  退出app
//
//
//
//
//                }else if (index==2){//到App Store下载页
//                    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",updateLink]];
//
//                    [[UIApplication sharedApplication] openURL:url];
//                }
//            };
//            [alext showXLAlertView];
//        }
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
